import { assetManager, ImageAsset, sp, Texture2D, url } from "cc";
import * as fgui from "fairygui-cc";
import UrlEnum from "./UrlEnum";
import { ViewManager } from "../../Game/module/base/ViewManager";
export default class UICore {
    private static _compressIgnoreList: Array<string> = [];

    /**
     * 加载资源列表
     */
    public static loadPackageList(list: Array<string>, callback?: Function): void {
        const url: string = list.pop();
        if (!url) {
            callback();
            return;
        }
        if (this._compressIgnoreList.indexOf(url) > -1) {
            UICore.loadPackageList(list, callback);
            return;
        }
        ViewManager.ins.showloading();
        fgui.UIPackage.loadPackage(`UI/${url}`, (error, pkg: fgui.UIPackage) => {
            ViewManager.ins.removeLoading();
            this._compressIgnoreList.push(url);
            UICore.loadPackageList(list, callback);
        })
    }
    // public static pushCompressIgnoreList(url: string): void {
    //     if (this._compressIgnoreList.indexOf(url) > -1) {
    //         return;
    //     }
    //     this._compressIgnoreList.push(url);
    // }
    public static addPackage(url): void {
        if (this._compressIgnoreList.indexOf(url) > -1) {
            return;
        }
        fgui.UIPackage.addPackage(`UI/${url}`);
        this._compressIgnoreList.push(url);
    }
    public static removePackage(url): void {
        fgui.UIPackage.removePackage(url);
        for (let i = 0; i < this._compressIgnoreList.length; i++) {
            if (this._compressIgnoreList[i] == url) {
                this._compressIgnoreList.splice(i, 1);
                break;
            }
        }

    }
    /**
    * 创建对象
    */
    public static createObject(pkgName: string, resName: string, userClass?: any): fgui.GObject {
        return fgui.UIPackage.createObject(pkgName, resName, userClass);
    }
    /**获取指定包下指定资源的地址 */
    public static getURL(pkgName: string, resName: string): string {
        return fgui.UIPackage.getItemURL(pkgName, resName);
    }
    /**注册扩展 */
    public static registerExtension(pkgName: string, resName: string, userClass: any): void {
        fgui.UIObjectFactory.setExtension(UICore.getURL(pkgName, resName), userClass);
    }
}